import { sentryVitePlugin } from "@sentry/vite-plugin";
import { defineConfig } from "vite";
import { loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import path from "path";

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: env.VITE_APP_PUBLIC_PATH,
    publicPath: env.VITE_APP_PUBLIC_PATH,
    plugins: [
      vue(),
      AutoImport({
        //注册
        imports: ["vue"],
        dts: "src/auto-import.d.ts",
      }),
      // sentryVitePlugin({
      //   org: "liu-mx",
      //   project: "javascript-vue"
      // })
    ],
    build: {
      cssCodeSplit: false,
      chunkSizeWarningLimit: 2048,
      outDir: env.VITE_APP_OUT_DIR || "dist",
      sourcemap: true,
      target: ["edge90", "chrome90", "firefox90", "safari15"],
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: "modern-compiler",
        },
      },
    },
    resolve: {
      alias: {
        // 设置路径
        "~": path.resolve(__dirname, "./"),
        // 设置别名
        "@": path.resolve(__dirname, "./src"),
      },
    },
    server: {
      // host: '0.0.0.0',
      cors: true,
      proxy: {
        "/fileuploadUrl/": {
          // target: 'https://exp.scjgj.sh.gov.cn/file_api/',
          target: "http://10.11.21.20:9072/",
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/fileuploadUrl\//, ""),
        },
        "/portalweb-api": {
          target: "http://10.11.21.20:19024/",
          // target: "http://172.17.8.252:19024/",
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/portalweb-api\//, ""),
          bypass(req, res, options) {
            const proxyURL = options.target + options.rewrite(req.url);
            console.log("proxyURL", proxyURL);
            req.headers["x-req-proxyURL"] = proxyURL; // 设置未生效
            res.setHeader("x-req-proxyURL", proxyURL); // 设置响应头可以看到
          },
        },
        "/portal/api": {
          target: "http://10.11.21.20:19024/",
          // target: "http://172.17.15.252:19024/",
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/portal\/api\//, "wl/"),

          bypass(req, res, options) {
            const proxyURL = options.target + options.rewrite(req.url);
            console.log("proxyURL", proxyURL);
            req.headers["x-req-proxyURL"] = proxyURL; // 设置未生效
            res.setHeader("x-req-proxyURL", proxyURL); // 设置响应头可以看到
          },
        },
        "/login": {
          target: "http://10.11.21.20:19005/cxjl-backend",
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/login/, ""),
        },
      },
    },
  };
});
